package com.tengju.data.infrastructure.repository.convert;

import com.tengju.data.domain.model.user.DealerInfo;
import com.tengju.data.domain.model.user.UserId;
import com.tengju.data.domain.model.user.UserIdCode;
import com.tengju.data.infrastructure.persistence.mybatis.dataobject.DealerInfoDO;

import java.util.Optional;

/**
 * @Author: 李大仙
 * @Date: 2021/3/24 3:39 下午
 */
public class DealerInfoConvert {
    public static DealerInfo convert(DealerInfoDO dealerInfoDO) {
        if (dealerInfoDO == null) {
            return null;
        }
        return new DealerInfo(
                dealerInfoDO.getId() != null && dealerInfoDO.getId() > 0 ? new UserId(dealerInfoDO.getId()) : null,
                dealerInfoDO.getIdCode() != null && dealerInfoDO.getIdCode() > 0 ? new UserIdCode(dealerInfoDO.getIdCode()) : null,
                Optional.ofNullable(dealerInfoDO.getpIdCode())
                        .filter(pIdCode -> pIdCode > 0).map(UserIdCode::new).orElse(null),
                dealerInfoDO.getName(),
                dealerInfoDO.getMobile(),
                dealerInfoDO.getNickname(),
                dealerInfoDO.getCreatedAt(),
                dealerInfoDO.getHeadImage());
    }
}
